/*
 * This file is part of "Sorting Thoughts" project.
 *
 * Copyright (c) 2009 Hendrik Ebel
 * All Rights Reserved.
 *
 * Please visit http://code.google.com/p/sorting-thoughts/ for more information.
 *
 * --LICENSE NOTICE--
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; If not, see <http://www.gnu.org/licenses/>.
 * --LICENSE NOTICE--
 */
function StartAssistant(){
}

StartAssistant.prototype.setup = function(){
	this.attributes = {
     	modelProperty: 'progressValue'
 	};
    this.model = {
		progressValue: 0
    };
    
    this.controller.setupWidget('progressBar', this.attributes, this.model);
    this.complete = this.complete.bind(this);
    this.progress = 0; // the first step
    this.updater = window.setInterval(this.updateProgress.bind(this), 1000);
    
    Mojo.Event.listen(this.controller.get('progressBar'), Mojo.Event.progressComplete, this.complete);
};

StartAssistant.prototype.activate = function(event){
	if (SortingThoughts.isTouchPad()) {
		$('startWrapper').removeClassName("cover");
	    $('startWrapper').addClassName("coverTouchpad");
		
		if (this.controller.stageController.setWindowOrientation) {
        	this.controller.stageController.setWindowOrientation("right");
		}
    } 
};


StartAssistant.prototype.deactivate = function(event){
	Mojo.Log.info("StartAssistant deactivate");
};

StartAssistant.prototype.cleanup = function(){
	Mojo.Log.info("StartAssistant clean up");
    window.clearInterval(this.updater);
    Mojo.Event.stopListening(this.controller.get('progressBar'), Mojo.Event.progressComplete, this.complete);
};

StartAssistant.prototype.updateProgress = function() {
	
    if (this.progress > 20) {
        window.clearInterval(this.updater);
    }
	
    if (this.progress == 0) {
        Mojo.Log.info("Table thought created " + this.progress);
        
		SortingThoughts.dao.createThoughtTable();

        this.progress += 1;
    }
	
	if (this.progress == 1) {
        Mojo.Log.info("Table settings created " + this.progress);
        
		SortingThoughts.dao.createSettingsTable();
        
        this.progress += 1;
    }
	
	if (this.progress == 2) {
        Mojo.Log.info("Table lock created " + this.progress);
        
		SortingThoughts.dao.createLockTable();
        
        this.progress += 2;
    }
	
	if (this.progress == 4) {
        Mojo.Log.info("Table sync created " + this.progress);
        
		SortingThoughts.dao.createSyncTable();
        
        this.progress += 1;
    }
    
    if (this.progress == 5) {
        Mojo.Log.info("Add row style column " + this.progress);
        
		SortingThoughts.dao.addRowStyleColumn();
        
        this.progress += 1;
    }
	
	if (this.progress == 6) {
        Mojo.Log.info("Add last sync date column " + this.progress);
        
		SortingThoughts.dao.addLastSyncDateColumn();
        
        this.progress += 1;
    }
	
	if (this.progress == 7) {
        Mojo.Log.info("Add create date column " + this.progress);
        
		SortingThoughts.dao.addCreateDateColumn();
        
        this.progress += 1;
    }
	
	if (this.progress == 8) {
        Mojo.Log.info("Add time zone offset column for thoughts " + this.progress);
        
		SortingThoughts.dao.addTimeZoneOffsetColumnToThoughtTable();
        
        this.progress += 1;
    }
	
	if (this.progress == 9) {
        Mojo.Log.info("Add rich column for thoughts " + this.progress);
        
		SortingThoughts.dao.addRichColumnToThoughtTable();
        
        this.progress += 1;
    }
	
	if (this.progress == 10) {
        Mojo.Log.info("Add time zone offset column " + this.progress);
        
		SortingThoughts.dao.addTimeZoneOffsetColumn();
        
        this.progress += 1;
    }
	
	if (this.progress == 11) {
        Mojo.Log.info("Add sorting order column " + this.progress);
        
		SortingThoughts.dao.addSortingOrderColumn();
        
        this.progress += 1;
    }
	
	if (this.progress == 12) {
		Mojo.Log.info("Insert welcome content " + this.progress);

		SortingThoughts.dao.insertWelcomeContent();
        
        this.progress += 1;
	}
	
	if (this.progress == 13) {
		Mojo.Log.info("Update missing row style " + this.progress);
        
		SortingThoughts.dao.updateMissingRowStyle();
        
        this.progress += 1;
	}
	
	if (this.progress == 14) {
		Mojo.Log.info("Update missing create date " + this.progress);
        
		SortingThoughts.dao.updateMissingCreateDate();
        
        this.progress += 1;
	} 
	
	if (this.progress == 15) {
		Mojo.Log.info("Insert setting content " + this.progress);
        
		var now = new Date();
	    var localOffset = now.getTimezoneOffset() * 60000;
		var sortingOrder = SortingThoughts.dao.getCurrentSortingOrderName();
		SortingThoughts.dao.insertSettingsContent(localOffset, sortingOrder);
        
        this.progress += 1;
	}
	
	if (this.progress == 16) {
		Mojo.Log.info("Update missing time zone offset " + this.progress);
        
		SortingThoughts.dao.updateMissingTimezoneOffset();
        
        this.progress += 20;
	}
	
	this.controller.modelChanged(this.model);
	this.model.progressValue = this.model.progressValue + (this.progress / 20);
};

StartAssistant.prototype.complete = function(){
    Mojo.Controller.stageController.popScene("start");
    
	if (SortingThoughts.enableAppLock) {
		Mojo.Controller.stageController.pushScene({
	        transition: Mojo.Transition.zoom,
	        name: "lock"
	    });
	} else {
	    Mojo.Controller.stageController.pushScene({
	        transition: Mojo.Transition.zoom,
	        name: "overview"
	    });
	}	
};


